package com.frogs.ucenter.controller;

import java.util.HashMap;
import java.util.Map;

import com.frogs.component.permission.api.service.PermissionHelper;
import com.frogs.framework.base.web.BaseController3;
import com.frogs.framework.util.DWZResponse;
import com.frogs.framework.util.PageList;
import com.frogs.framework.util.RenderUtil;
import com.frogs.framework.util.StringUtil;
import com.frogs.framework.web.HttpRequestInfo;
import com.frogs.system.util.ApplicablePlatformEnum;
import com.frogs.system.util.ExpandCodeEnum;
import com.frogs.ucenter.entity.UserRechargeStrategyChoose;
import com.frogs.ucenter.service.UserRechargeStrategyChooseService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/admin/userRechargeStrategyChoose")
public class UserRechargeStrategyChooseController extends BaseController3 {
	private static final Logger log = LoggerFactory.getLogger(UserRechargeStrategyChooseController.class);
	
	@Resource(name = "permissionHelper")
	private PermissionHelper permissionHelper;
	
    private static final String ROOT_VIEW = "admin/userRechargeStrategyChoose";

    private static final String HOME_VIEW = "admin/userRechargeStrategyChoose/home";

    private static final String EDIT_VIEW = "admin/userRechargeStrategyChoose/edit";

    @Autowired
    private UserRechargeStrategyChooseService userRechargeStrategyChooseService;

    @RequestMapping()
    public String listUserRechargeStrategyChoose(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, true );
        Integer applicablePlatformId = reqInfo.getIntParameter( "eqApplicablePlatformId", 0 );
        model.put("eqApplicablePlatformId", applicablePlatformId);
        getPageList( model );
        return HOME_VIEW;
    }

    @RequestMapping(EDIT)
    public String createUserRechargeStrategyChoose(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap( request, model, false );
        Integer type = reqInfo.getIntParameter( "type", -1 );
        if( type > 0 ){
            model.put( "model",userRechargeStrategyChooseService.getById( type ) ); 
        }
        return EDIT_VIEW;
    }

    @RequestMapping(ADD)
    public String saveUserRechargeStrategyChoose(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserRechargeStrategyChoose userRechargeStrategyChoose, @RequestParam(required=true, value="ids") String inIdList) {
        setRequestModelMap(request, model, false);
        DWZResponse.Builder builder = null;
        try {
        	Map<String,Object> map = new HashMap<String,Object> ();
        	String[] idArray = inIdList.split(",");
            if(!StringUtil.isEmpty(idArray)){
            	if(idArray.length>1){
            		builder = DWZResponse.getFailBuilder("保存失败,只能选择一种策略");
            		RenderUtil.renderHtml(builder.build().toString(), response);
            		return null;
            	}
            	int userId = permissionHelper.getUser().getUserId();
            	int typeId = Integer.parseInt(model.get("eqTypeId").toString());
            	map.put("userId", userId);
            	map.put("typeId", typeId);
            	map.put("subId", Integer.parseInt(idArray[0]));
            	String expandCode = model.get("eqExpandCode").toString();
            	if(StringUtil.isEmpty(expandCode)){
            		expandCode = ExpandCodeEnum.ALL.getId();
            	}
            	map.put("expandCode", expandCode);
            	int applicablePlatformId = Integer.parseInt(model.get("eqApplicablePlatformId").toString());
            	if(StringUtil.isEmpty(applicablePlatformId)){
            		applicablePlatformId = ApplicablePlatformEnum.ALL.getId();
            	}
            	map.put("applicablePlatformId", applicablePlatformId);
            	userRechargeStrategyChooseService.insert(map);
            	builder = DWZResponse.getSucessBuilder("保存成功");
            }
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("保存失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        return null;
    }

    @RequestMapping(SHOW)
    public String showUserRechargeStrategyChoose(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer type = reqInfo.getIntParameter( "type", -1 );
        if( type > 0 ){
            model.addAttribute( userRechargeStrategyChooseService.getById( type ) ); 
        }
        setRequestModelMap(request, model);
        return null;
    }

    @RequestMapping(UPDATE)
    public String updateUserRechargeStrategyChoose(HttpServletRequest request, HttpServletResponse response, ModelMap model, UserRechargeStrategyChoose userRechargeStrategyChooseNew) throws Exception {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer type = reqInfo.getIntParameter( "type", -1 );
        if( type > 0 ){
            DWZResponse.Builder builder;
            try {
                UserRechargeStrategyChoose userRechargeStrategyChoose = userRechargeStrategyChooseService.getById( type ); 
                PropertyUtils.copyProperties(userRechargeStrategyChoose, userRechargeStrategyChooseNew);
                userRechargeStrategyChooseService.update( userRechargeStrategyChoose ); 
                model.clear();
                builder = DWZResponse.getSucessBuilder("更新成功");
            } catch (Exception e) {
                builder = DWZResponse.getFailBuilder("更新失败" + e.getMessage());
            }
            RenderUtil.renderHtml(builder.build().toString(), response);
        }
        return null;
    }

    @RequestMapping(DELETE)
    public String deleteUserRechargeStrategyChoose(HttpServletRequest request, HttpServletResponse response, ModelMap model, String inIdList) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        DWZResponse.Builder builder;
        try {
            String[] idArray = inIdList.split(",");
            model.put("inIdList", idArray);
            userRechargeStrategyChooseService.delete( model ); 
            builder = DWZResponse.getSucessBuilder("删除信息成功");
        } catch (Exception e) {
            builder = DWZResponse.getFailBuilder("删除信息失败" + e.getMessage());
        }
        RenderUtil.renderHtml(builder.build().toString(), response);
        model.clear();
        return null;
    }

    private PageList<UserRechargeStrategyChoose> getPageList(ModelMap model) {
    	if(null == model.get("eqTypeId")){
    		model.put("eqTypeId", 1);
    	}
    	if(null == model.get("eqExpandCode")){
    		model.put("eqExpandCode", ExpandCodeEnum.ALL.getId());
    	}
    	if(null == model.get("eqApplicablePlatformId")){
    		model.put("eqApplicablePlatformId", ApplicablePlatformEnum.ALL.getId());
    	}
        PageList<UserRechargeStrategyChoose> userRechargeStrategyChooseList = userRechargeStrategyChooseService.getList( model, Integer.parseInt(model.get("pageNum").toString()), Integer.parseInt(model.get("numPerPage").toString()) ); 
        model.put("userRechargeStrategyChooseList",userRechargeStrategyChooseList);
        model.put("pageTurn",userRechargeStrategyChooseList.getPageTurn());
        return userRechargeStrategyChooseList;
    }
}